package com.charlie.meetingmanagement.controller;


import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Request;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.requests.AddRequest;
import com.charlie.meetingmanagement.service.RequestService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin(origins = "*")
public class RequestController {

    @Resource
    private RequestService requestService;
    //前台根据用户ID 来获取请求会议室的情况的
    @PostMapping("/request/getRentalRequestById")
    public Result getRentalRequestById(@RequestParam Integer  userId){
        List<Request> rentalRequest=requestService.getRentalRequestById(userId);
        System.out.println(rentalRequest);
        return Result.success(rentalRequest);
    }
    //前台 更具用户 id 请求会议室
    @PostMapping("/request/addRequest")
    public Result addRequest(@RequestBody AddRequest addRequest){
        Integer userId = addRequest.getUserId();
        Integer meetingroomId = addRequest.getMeetingroomId();
        LocalDate meetingroomRentaldate = addRequest.getMeetingroomRentaldate();

        Integer meetingroomRentaltime = addRequest.getMeetingroomRentaltime();
        System.out.println(meetingroomRentaltime);
        String rentalDescribe = addRequest.getRentalDescribe();
        requestService.addRequest(userId,meetingroomId,meetingroomRentaldate,meetingroomRentaltime,rentalDescribe);
        return Result.success();
    }
    //    条件分页获取request数据
    @GetMapping("/request/requestInfoMore")
    public Result requestInfoMore(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam(required = false) Integer userId,
                                  @RequestParam(required = false) Integer rentalId,
                                  @RequestParam(required = false) Integer meetingroomId,
                                  @RequestParam(required = false) String meetingroomRentaldateTemp,
                                  @RequestParam(required = false) Integer meetingroomRentaltime,
                                  @RequestParam(required = false) Integer approval)
    {
        log.info("分页查询request数据");
        LocalDate meetingroomRentaldate = null;
        if (meetingroomRentaldateTemp != null)
            meetingroomRentaldate = LocalDate.parse(meetingroomRentaldateTemp);
        PageBean pageBean = requestService.page(page,pageSize,userId,rentalId,meetingroomId,meetingroomRentaldate,
                meetingroomRentaltime,approval);
        return Result.success(pageBean);
    }

//    通过请求ID对审批结果Approval进行修改,若修改为1，即通过，需要将其加到meetingroom_rental表中
//    需要注意的是，如若对本就通过或驳回的request再次发起请求，会返回error错误
    @PostMapping("/request/updateApprovalById")
    public Result approveRequest(@RequestParam Integer approveId,@RequestParam Integer rentalId,@RequestParam Integer approval){
        log.info("start to approveRequest");
        return requestService.approveRequest(approveId,rentalId,approval);
    }
}
